<?php
namespace wechat\controllers;

use Yii;
use yii\base\Event;
use yii\helpers\Url;
use framework\helpers\Utils;
use yii\web\Controller;

/**
 * Site controller
 */
class WithdrawController extends Controller
{
	public $layout = 'layout';
    public function actionIndex()
    {
    	$session = yii::$app->session;
    	$session->open();
    	if($session->has('sKey'))	//是否有sKey缓存
    	{
    		$sKey = $session->get('sKey');
    		$url = PANDA_SERVER.'/getUserSecurityInfo?sKey='.$sKey.'&tokenId='.Utils::getTokenId();
    		$result = file_get_contents($url);
    		$result_arr = json_decode($result,true); // JSON结果字符串转数组
    		if ($result_arr['status']!=0)	//sKey过期
    		{
    			return $this->redirect(['site/signin']);
    		}
    		else 	//已登录
    		{
    			$url = PANDA_SERVER.'/getBindingBank?sKey='.$sKey.'&tokenId='.Utils::getTokenId();	//获取用户已保存的银行账户信息。
    			$_result = file_get_contents($url);
    			$_result_arr = json_decode($_result,true); // JSON结果字符串转数组
    			if ($_result_arr['status'] == 0)	//判断是否返回了正常的记录列表
    			{
    				$userinfos = $_result_arr['obj'];
    			}
    			else
    			{
    				$userinfos = null;
    			}
    			return $this->render('index',compact('userinfos'));
    		}
    	}
    	else
    	{
    		return $this->redirect(['site/signin']);
    	}
    }
    
    public function actionDowithdraw()
    {
    	if ($_POST)
    	{
    		$money = $_POST['jine'];
    		$tipwd = $_POST['tipwd'];
    		$session = yii::$app->session;
    		$session->open();
    		if($session->has('sKey'))	//是否有sKey缓存
    		{
    			$sKey = $session->get('sKey');
    			$url = PANDA_SERVER.'/getUserSecurityInfo?sKey='.$sKey.'&tokenId='.Utils::getTokenId();
    			$result = file_get_contents($url);
    			$result_arr = json_decode($result,true); // JSON结果字符串转数组
    			if ($result_arr['status']!=0)	//sKey过期
    			{
    				return $this->redirect(['site/signin']);
    			}
    			else 	//已登录
    			{
    				$url = PANDA_SERVER.'/withdarw?sKey='.$sKey.'&money='.$money.'&pwd='.$tipwd.'&tokenId='.Utils::getTokenId();	//获取用户已保存的银行账户信息。
    				$_result = file_get_contents($url);echo $_result;exit;
    				$_result_arr = json_decode($_result,true); // JSON结果字符串转数组
    				if ($_result_arr['status'] == 0)	//判断是否返回了正常的记录列表
    				{
    					$userinfos = $_result_arr['obj'];
    				}
    				else
    				{
    					$userinfos = null;
    				}
    				return $this->render('index',compact('userinfos'));
    			}
    		}
    		else
    		{
    			return $this->redirect(['site/signin']);
    		}
    	}
    }
}